package com.inputabc.ct.v1.ui.inputbox.action;

import com.inputabc.EzGUIFramework.util.alias.Ez;
import com.inputabc.ct.v1.context.ContextParams;
import com.inputabc.ct.v1.ui.inputbox.InputBoxBuilder;
import com.inputabc.ct.v1.ui.inputbox.component.InputBoxCloseButtonFrame;
import com.inputabc.ct.v1.ui.inputbox.component.InputBoxFrame;
import com.inputabc.ct.v1.ui.inputbox.component.InputBoxTextArea;
import org.apache.commons.lang3.StringUtils;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;

public class InputBoxAction {
    private InputBoxFrame inputBoxFrame = ((InputBoxBuilder) ContextParams.contextParam.get("inputBoxBuilder")).getInputBoxFrame();
    private InputBoxCloseButtonFrame inputBoxCloseButtonFrame = ((InputBoxBuilder) ContextParams.contextParam.get("inputBoxBuilder")).getInputBoxCloseButtonFrame();

    public void copy(InputBoxTextArea inputBoxTextArea) {
        inputBoxTextArea.copy();
    }

    public void cut(InputBoxTextArea inputBoxTextArea) {
        inputBoxTextArea.cut();
    }

    public void paste(InputBoxTextArea inputBoxTextArea) {
        inputBoxTextArea.paste();
    }

    public void enterDown(KeyEvent keyEvent) {
        InputBoxTextArea inputBoxTextArea = (InputBoxTextArea) keyEvent.getSource();
        int keyCode = keyEvent.getKeyCode();
        if (keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {

            String text = inputBoxTextArea.getText();
            text = text.trim();
            Clipboard systemClipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            if (StringUtils.isNotBlank(text)) {
                systemClipboard.setContents(new StringSelection(text), null);
            }
        }

    }

    public void closing(WindowEvent windowEvent) {
        Window window = (Window) windowEvent.getSource();
        Ez.fadeOut(window, Ez.FADE_FAST);

    }
    public void opening(WindowEvent windowEvent){
        Window window = (Window) windowEvent.getSource();
        Ez.fadeIn(window, Ez.FADE_FAST);
    }

    public void pushCloseButton(MouseEvent mouseEvent) {
        if (mouseEvent.getButton() == MouseEvent.BUTTON1) {
            inputBoxFrame.dispose();
            inputBoxCloseButtonFrame.dispose();
        }
    }
}
